# !/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import xlrd
import importlib
from modelDb.tourists import touristsModel
from modelDb.tourists_actions import touristsActionsModel
from modelDb.tourists_info import touristsInfoModel
import redis
from config.db import redis as redisConf

#=============================================================
# dmp etl
#=============================================================
class importExcelMain(object):
    def __init__(self):
        """
        初始化
        """
        self.modelEtl = None
        self.source = {
            1 : "taiyue"
        }



    def start(self):
        """
        入口
        :return:
        """
        try:
            #todo redis获取上传的文件名
            uploadFile = "/alidata/wwwroot/zhuge-dmpadmin/storage/app/tourists/1/20170606/2b0587572e1eb837e4f111af6d763e13.xlsx"
            sourceId = 1

            #todo 判断文件是否存在
            if not os.path.exists(uploadFile):
                print "上传文件不存"
                exit()

            # todo 文件权限
            if not os.access(uploadFile, os.R_OK):
                print "上传文件没有读权限"
                exit()

            # excel读取
            data = xlrd.open_workbook(uploadFile)
            table = data.sheets()[0]
            nrows = table.nrows

            #redis
            redisConn = redis.StrictRedis(host=redisConf.get("host"), port=redisConf.get("port"), db=redisConf.get("db"), password=redisConf.get("auth"))

            # TODO 客源数据
            self.touristsActions = touristsActionsModel()
            self.tourists = touristsModel()
            self.touristsInfoM = touristsInfoModel()
            self.touristsUser = {} #客源数据，优化查询，查询过之后不在查询
            self.touristsInfo = {}
            resultList = []
            for i in xrange(0, nrows):
                if i == 0: continue
                rowValues = table.row_values(i)
                structure = self.getStructuredData(etlData=rowValues, sourceId=sourceId, redisConn=redisConn)
                self.touristsSelect(structure)
                resultList.append(structure.getDictData())
                print "第%d行"


            self.touristsActions.addMany(resultList)
            self.touristsActions = None
            self.tourists = None
            self.touristsUser = None
            print "操作完成"
        except Exception, e:
            print e

    def getStructuredData(self, etlData, sourceId, redisConn):
        """
        获取结构化数据
        :return:
        """
        data = etlData
        source = self.source[sourceId]
        if not self.modelEtl:
            etlModelPath = "modelEtl.%s" % (source)
            self.modelEtl = importlib.import_module(etlModelPath)

        etlClass = getattr(self.modelEtl, source)
        return etlClass(source=sourceId, data=data, redis=redisConn)


    def touristsSelect(self, structure):
        """
        客源管理
        :return:
        """
        phone = structure.phone
        if not self.touristsUser.has_key(phone):
            # todo 查询
            touristsData = self. tourists.findByPhone(phone)
            if not touristsData:
                structure.user_id = self.tourists.add(structure.getDictData())
                self.touristsInfoM.add(structure.getDictData())
                touristsData = {"id":structure.user_id, "etl_status":structure.etl_status}

            self.touristsInfo[phone] = self.touristsInfoM.findByPhone(phone)
            self.touristsUser[phone] = touristsData


        #客源详情
        touristsInfoData = self.touristsInfo[phone]
        self.touristsInfoM.saveImport(phone, touristsInfoData, structure.getDictData())


        touristsData = self.touristsUser[phone]
        structure.user_id = touristsData.get("id")

        # 已经清洗完成的数据，直接流入画像
        # if touristsData.get("etl_status") != 1:
        #     structure.data_status = 2  # 清洗完成
        return True


if __name__ == "__main__":
    importExcelMain().start()
